home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / a_utils / perl / prlbkxmp.lha / ch2 / dodir < prev    next >
Text File  |  1991-01-07  |  1KB  |  51 lines

  1. #!/usr/bin/perl
  2.  
  3. # Start at the top.
  4.  
  5. &dodir('.');
  6.  
  7. sub dodir {
  8.     local($dir,$nlink) = @_;
  9.     local($dev,$ino,$mode,$subcount);
  10.  
  11.     # At the top level, we need to find nlink ourselves.
  12.  
  13.     ($dev,$ino,$mode,$nlink) = stat('.') unless $nlink;
  14.  
  15.     # Get the list of files in the current directory.
  16.  
  17.     opendir(DIR,'.') || die "Can't open $dir";
  18.     local(@filenames) = readdir(DIR);
  19.     closedir(DIR);
  20.  
  21.     if ($nlink == 2) {        # This dir has no subdirectories.
  22.     for (@filenames) {
  23.         next if $_ eq '.';
  24.         next if $_ eq '..';
  25.         print "$dir/$_\n";
  26.     }
  27.     }
  28.     else {                    # This dir has subdirectories.
  29.     $subcount = $nlink - 2;
  30.     for (@filenames) {
  31.         next if $_ eq '.';
  32.         next if $_ eq '..';
  33.         $name = "$dir/$_";
  34.         print $name,"\n";
  35.         next if $subcount == 0;    # Seen all the subdirs?
  36.  
  37.         # Get link count and check for directoriness.
  38.  
  39.         ($dev,$ino,$mode,$nlink) = lstat($_);
  40.         next unless -d _;
  41.  
  42.         # It really is a directory, so do it recursively.
  43.  
  44.         chdir $_ || die "Can't cd to $name";
  45.         &dodir($name,$nlink);
  46.         chdir '..';
  47.         --$subcount;
  48.     }
  49.     }
  50. }
  51.